require(['grid', 'topBar', 'layout','select2'], function () {
    var editDialog,searchLineCar, $top = $('#top'),$grid = $('#grid'), selLine=[],lineIdsStr,carIdsStr,reloadGridData;

    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                   {
                       text: '是否已绑定驾驶员',
                       name: 'binding',
                       width: 80,
                       type: 'select',
                       dict:'BIN00000'
                   },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                    	searchLineCar();
                    }
                }
            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                            	exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                            	exportFile('Pdf');
                            }
                        }
                    ]
                },
                {
                    text: '+分配车辆',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
        				var nodes = commonFun.getLeftTreeCheckedNodes();
        				var includeLine = false,isSoloLine=true;
        				//判断左侧树是否选择相应节点 判断是否打开弹出框进行操作
        				for(var key in nodes){
        					if(key=='LINE'){
        						includeLine = true;
        						selLine = nodes[key];
        						if(selLine.length>1){
        							isSoloLine = false;
        						}
        					}
        				}
        				if(includeLine){//是否包含线路
        					if(!isSoloLine){
        						$.messager.alert("警告","请选择单条线路!","info");
        					}else{
        						getEditDialog().dialog('exec', 'reloadCarList',selLine.toString());
        					}
        				}else{
        					$.messager.alert("警告","请选择线路!","info");
        				}
                    }
                }
            ]
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/lineCarRel/searchLineCarRel.htm",
            colModel: [
                {name: "relId",label: 'id', key: true, hidden: true},
                {name: "lineId",label: 'lineId',hidden: true},
                {name: "carId",label: 'carId', hidden: true},
                {name: "lineName",label: '线路名称', width: 100},
                {name: "licPltNo",label: '车牌号', width: 100},
                {name: "eqmtCd",label: '设备号', width: 100},
                {name: "carType",label: '车辆类型', width: 100,dict: "CAT00000"},
                {name: "operCertiNo",label: '运营证号', width: 100},
                {name: "binding",label: '驾驶员绑定状态', width: 100,dict: "BIN00000"},
                {name: "remark",label: '备注', width: 200},
                {name: "operate", width: 100, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '删除关联',
                        css:'operate-delete',
                        click: function (data) {
                            $.messager.confirm('删除', "线路:  "+  data.lineName + "将解除车牌号为:  " +  data.licPltNo + "的车辆,确实解除吗?", 'delete', function (r) {
                                if (r) {
                                	$.ajaxExt({
            							url : "/lineCarRel/delLineCarRel.htm",
            							data : {'relId' : data.relId,'lineName' : data.lineName,'licPltNo' : data.licPltNo,'carId' : data.carId}
            						}).done(function(){
            							reloadGridData(lineIdsStr,"");
                                        commonFun.reloadLeftTree();
            						});
                                }
                            });
                        }
                    }]
                }
            ],
            multiselect: false
        });
        
    }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog() {
    	if(editDialog){
    		editDialog.dialog('open')
    	}else{
            editDialog = $('<div>').dialog({
                title: '分配车辆',
                url: './master/setting/sysLineCarSetEdit.htm',
                height: 550,
                width: 1020,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                    		    getEditDialog().dialog('exec', 'add',function(){
                    		    	getEditDialog().dialog('close');
                    		    	var lineStr = commonFun.getLeftTreeCheckedNodes("LINE").toString();
                                    commonFun.reloadLeftTree();
                    		    	reloadGridData(lineStr,"");
                    		    });
                        }
                    },
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
    	}
        return editDialog;
    }
    
    //查询
    searchLineCar = function (){
    	var nodes = commonFun.getLeftTreeCheckedNodes();
		for(var key in nodes){
			//选择线路
			if(key=='LINE'){
				lineIdsStr = nodes[key].toString();
			}
			//选择车辆
			if(key=='CAR'){
				carIdsStr = nodes[key].toString();
			}
		}
		//判断左侧树是否选择节点，选择了就刷新车线关系列表
		if(nodes.CAR||nodes.LINE){
			reloadGridData(lineIdsStr,carIdsStr);
		}else{
			$.messager.alert("警告","请选择线路/车辆!","info");
		}
    };
    
	//载入线路车辆绑定
	reloadGridData = function (lines,cars){
		$grid.grid('setGridParam', {page:1,datatype:"json",postData: {"lineIds":lines,"carIds":cars,
			"binding":$top.getValues().binding}}).trigger("reloadGrid");
	};
	
	//导出
	function exportFile(suffix){
		if($grid.getGridParam("reccount")>0){
			var postData = {};
		    postData = $top.getValues();
		    postData.lineIds = lineIdsStr;
		    postData.carIds = carIdsStr;
			if(typeof(carIdsStr) == 'undefined'){ // 左侧树没有选中车辆
				$.messager.alert("提示","请在左侧树中选择车辆，再点击查询。","info");
			}else{
	    		$.exportFile("/lineCarRel/exportLineCarRel"+suffix+".htm",postData);
			}
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
});